<?php


namespace App\Exceptions\JsonResponse;


use App\Exceptions\Code\ErrorCode;
use Illuminate\Validation\ValidationException;

class ResponseValidationException extends JsonResponseException
{
    public function __construct(string $message = null, ?int $code = ErrorCode::VALIDATION_FAILED, ValidationException $previous = null, array $headers = [])
    {
        parent::__construct($message, $code, $previous, $headers);
        $this->message = $this->getValidationErrorString($previous);
    }

    protected function getValidationErrorString(ValidationException $previous)
    {
        return array_reduce($previous->errors(), function ($result, $v) {
            $result .= implode(';', $v) . ';';
            return $result;
        });
    }
}
